package result;

/**
 * @Author:princess
 * @Date:2024/10/9-10-09-10:44
 * 全平台统一响应结果
 */

public class Result<T> {
    private Integer code;
    private String msg;
    private T data;

    // 构造函数
    protected Result() {}

    // Getter and Setter
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    // 静态工厂方法
    public static <T> Result<T> success(T data) {
        return new ResultBuilder<>(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> error(ResultCode code, String msg) {
        return new ResultBuilder<>(code, msg);
    }

    // 内部构建器类
    protected static class ResultBuilder<T> extends Result<T> {
        private ResultBuilder(ResultCode code, T data) {
            setCode(code.getCode());
            setMsg(code.getMessage());
            setData(data);
        }

        private ResultBuilder(ResultCode code, String msg) {
            setCode(code.getCode());
            setMsg(msg);
        }
    }
}

